/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package beans;

import database_beans.Katedra;
import database_beans.Predmet;
import java.io.Serializable;
import java.util.ArrayList;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;
import server.Log;
import server.Util;

/**
 *
 * @author stefan
 */
@ManagedBean
@SessionScoped
public class TableKatedre implements Serializable {

    private ArrayList<ArrayList<Predmet>> semestri = new ArrayList<ArrayList<Predmet>>();

    private Predmet selectedPredmet;

    private static int katedra;

    /**
     * Creates a new instance of TablePredmetiBean
     */
    public TableKatedre() {

    }

    public String loadRti() {
        semestri.clear();
        Log.i("Je l uopste usao u jebeni load");
        Session session = Util.getSesion();
        Transaction t = session.beginTransaction();
        try{
        Criteria katedraCriteria = session.createCriteria(Katedra.class);
        katedraCriteria.add(Restrictions.eq("opis", "RTI"));
        Katedra katedra = (Katedra) katedraCriteria.list().get(0);
        Criteria predmetCriteria;
        //Katedra k = new Katedra("RTI");
        //userCriteria.add(Restrictions.eq("katedra", 1));
        for (int i = 3; i < 9; i++) {
            predmetCriteria = session.createCriteria(Predmet.class);
            predmetCriteria.add(Restrictions.eq("semestar", Integer.toString(i)));
            predmetCriteria.add(Restrictions.eq("katedra", katedra));
            //Log.i("Broj nadjenih predmeta RTI katedre u semestru" + i + " je " + predmetCriteria.list().size());
            semestri.add((ArrayList<Predmet>) predmetCriteria.list());
        }

        t.commit();
        return "rti?faces-redirect=true";
        } catch (Exception e) {
            e.printStackTrace();
            t.rollback();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Greska! Neuspesna konekcija sa bazom"));
        }
        return "";

    }

    public String loadSi() {

        semestri.clear();
        Session session = Util.getSesion();
        Transaction t = session.beginTransaction();
        try {
            Criteria userCriteria;
            Criteria katedraCriteria = session.createCriteria(Katedra.class);
            katedraCriteria.add(Restrictions.eq("opis", "SI"));
            Katedra katedra = (Katedra) katedraCriteria.list().get(0);
            //userCriteria.add(Restrictions.eq("katedra", 1));
            for (int i = 3; i < 9; i++) {
                userCriteria = session.createCriteria(Predmet.class);
                userCriteria.add(Restrictions.eq("semestar", Integer.toString(i)));
                userCriteria.add(Restrictions.eq("katedra", katedra));
               // Log.i("Broj nadjenih predmeta SI katedre u semestru" + i + " je " + userCriteria.list().size());
                semestri.add((ArrayList<Predmet>) userCriteria.list());
            }
            t.commit();

        } catch (Exception e) {
            t.rollback();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Greska! Neuspesna konekcija sa bazom"));
        }
        return "si?faces-redirect=true";
    }

    public String loadMaster() {
        semestri.clear();
        Session session = Util.getSesion();
        Transaction t = session.beginTransaction();
        try {
            Criteria userCriteria;
            Criteria katedraCriteria = session.createCriteria(Katedra.class);
            katedraCriteria.add(Restrictions.eq("opis", "RTI"));
            Katedra katedra = (Katedra) katedraCriteria.list().get(0);
            //userCriteria.add(Restrictions.eq("katedra", 1));
            for (int i = 9; i < 11; i++) {
                userCriteria = session.createCriteria(Predmet.class);
                userCriteria.add(Restrictions.eq("semestar", Integer.toString(i)));
                userCriteria.add(Restrictions.eq("katedra", katedra));
               // Log.i("Broj nadjenih predmeta RTi katedre na masteru" + i + " je " + userCriteria.list().size());
                semestri.add((ArrayList<Predmet>) userCriteria.list());
            }
            t.commit();

        } catch (Exception e) {
            t.rollback();
        }
        return "master?faces-redirect=true";
    }

    public String loadDoktorske() {
        semestri.clear();
        Session session = Util.getSesion();
        Transaction t = session.beginTransaction();
        try {
            Criteria userCriteria;
            Criteria katedraCriteria = session.createCriteria(Katedra.class);
            katedraCriteria.add(Restrictions.eq("opis", "SI"));
            Katedra katedra = (Katedra) katedraCriteria.list().get(0);
            //userCriteria.add(Restrictions.eq("katedra", 1));
            for (int i = 11; i < 12; i++) {
                userCriteria = session.createCriteria(Predmet.class);
                userCriteria.add(Restrictions.eq("semestar", Integer.toString(i)));
                userCriteria.add(Restrictions.eq("katedra", katedra));
                //Log.i("Broj nadjenih predmeta RTI katedre na doktorskim" + i + " je " + userCriteria.list().size());
                semestri.add((ArrayList<Predmet>) userCriteria.list());
            }
            t.commit();

        } catch (Exception e) {
            t.rollback();
        }
        return "doktorske?faces-redirect=true";
    }

    public ArrayList<ArrayList<Predmet>> getSemestri() {
        return semestri;
    }

    public void setSemestri(ArrayList<ArrayList<Predmet>> semestri) {
        this.semestri = semestri;
    }

    public Predmet getSelectedPredmet() {
        return selectedPredmet;
    }

    public void setSelectedPredmet(Predmet selectedPredmet) {
        this.selectedPredmet = selectedPredmet;
    }

    public String onPredmetSelected() {
        return "welcomePrimefaces";
    }

    public void putKatedraIndicator(int katedra) {
        Log.i("putKatedraIndicator");
        this.katedra = katedra;
    }

}
